/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.service.cud.impl;

import java.io.File;
import java.io.IOException;

import net.iwsh.core.utils.IDGenerator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.us.usws.biz.ResourceStorageReplacer;
import com.us.usws.dao.SchoolDAO;
import com.us.usws.model.domain.system.School;
import com.us.usws.model.domain.video.VideoFile;
import com.us.usws.model.flat.system.DOSchool;
import com.us.usws.service.cud.SchoolCUDService;

/**
 * Service RO impl class
 * @author robot
 */
public class SchoolCUDServiceImpl implements SchoolCUDService {

	/**
	 * DAO instance
	 */
	private SchoolDAO dao = new SchoolDAO();

	/**
	 * Insert one row.
	 */
	@Override
	public String addSchool(DOSchool school) {
		return dao.insertSchool(school);
	}
	
	/**
	 * Insert one row.
	 */
	@Override
	public boolean addSchool(School school, VideoFile videoFile) {
		
		/*
		 * create video file uid
		 */
		String videoFileUid = IDGenerator.getInstance().genUID();
		videoFile.setUid(videoFileUid);
		
		/*
		 * create school uid and set file uid for school
		 */
		String videoUid = IDGenerator.getInstance().genUID();
		school.setUid(videoUid);
		school.setVideoFileUid(videoFileUid);
		
		String snapshotPath = videoFile.getSnapshot();
		String videoPath = videoFile.getVideoPath();
		try {
			if(StringUtils.isNotBlank(snapshotPath)) {
              	int index = snapshotPath.lastIndexOf("/");
              	if(index != -1) {
              		videoFile.setSnapshot(snapshotPath.substring(index+1));
              		FileUtils.copyFile(new File(snapshotPath), new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot())));
              	} 
             }
			if(StringUtils.isNotBlank(videoPath)) {
				FileUtils.copyFile(new File(videoPath), new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt())));
			}
			
			/*
			 * when file copy is success, then insert video info
			 */
			return dao.insertSchool(school, videoFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Update by PK.
	 */
	@Override
	public void updateSchool(School school, VideoFile videoFile) {
		
		String snapshotPath = videoFile.getSnapshot();
		try {
			if(StringUtils.isNotBlank(snapshotPath) && snapshotPath.contains("/")) {
				int index = snapshotPath.lastIndexOf("/");
			  	if(index != -1) {
			  		videoFile.setSnapshot(snapshotPath.substring(index+1));
			  		FileUtils.copyFile(new File(snapshotPath), new File(ResourceStorageReplacer.getVideoFileSnapshotPath(videoFile.getUid(), videoFile.getSnapshot())));
			  	} 
			}
			String videoPath = videoFile.getVideoPath();
			if(StringUtils.isNotBlank(videoPath) && videoPath.contains("/")) {
				FileUtils.copyFile(new File(videoPath), new File(ResourceStorageReplacer.getVideoFilePath(videoFile.getUid(), videoFile.getFileExt())));
			}
			dao.updateSchool(school, videoFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	/**
	 * Update by PK.
	 */
	@Override
	public void updateSchool(DOSchool school) {
		dao.updateSchool(school);
	}

	/**
	 * Update is valid flag by PK.
	 */
	@Override
	public void updateIsValid(String isValid, String uid) {
		dao.updateIsValid(isValid, uid);
	}

	/**
	 * Delete row by PK.
	 */
	@Override
	public void deleteSchool(String uid) {
		dao.deleteSchool(uid);
	}

	@Override
	public void deleteSchoolMore(String uids) {
		if (StringUtils.isNotBlank(uids)) {
			String[] uid = StringUtils.split(uids, ",");
			for (String id : uid) {
				deleteSchool(id);
			}
		}
	}

}